var crypto = require("./crypto.js")
var constants = require("../constants.js")
var slots = require("../time/slots.js")
var options = require('../options')

function calculateFee(amount) {
  return constants.fees.send;
  // var min = constants.fees.send;
  // var fee = Number(parseFloat((amount * constants.fees.percent).toFixed(0)));
  // return fee < min ? min : fee;
}

function createTransaction(recipientId, amount, message, secret, secondSecret) {
  var transaction = {
    type: 0,
    amount: Number(amount),
    fee: calculateFee(amount),
    recipientId: recipientId,
    message: message,
    timestamp: slots.getTime() - options.get('clientDriftSeconds'),
    asset: {}
  };

  var keys = crypto.getKeys(secret);
  transaction.senderPublicKey = keys.publicKey;

  crypto.sign(transaction, keys);

  if (secondSecret) {
    var secondKeys = crypto.getKeys(secondSecret);
    crypto.secondSign(transaction, secondKeys);
  }

  transaction.id = crypto.getId(transaction);
  return transaction;
}

function createLock(height, secret, secondSecret) {
  var transaction = {
    type: 100,
    amount: 0,
    fee: constants.fees.lock,
    recipientId: '',
    args: [String(height)],
    timestamp: slots.getTime() - options.get('clientDriftSeconds'),
    asset: {}
  };

  var keys = crypto.getKeys(secret);
  transaction.senderPublicKey = keys.publicKey;

  crypto.sign(transaction, keys);

  if (secondSecret) {
    var secondKeys = crypto.getKeys(secondSecret);
    crypto.secondSign(transaction, secondKeys);
  }

  transaction.id = crypto.getId(transaction);
  return transaction;
}

module.exports = {
  createTransaction: createTransaction,
  calculateFee: calculateFee,
  createLock: createLock
}